<?php

namespace app\admin\controller;


class Admin extends Base
{
    //管理员列表
    public function adminlist()
    {
        $admin = model('common/Admin')
            -> order('status','asc')
            -> paginate(10);
        $info = [
            'admin' => $admin
        ];
        $this -> assign($info);
        return view();
    }

    //管理员添加
    public function adminadd()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $result = model('common/Admin') -> adminadd($data);
            if ($result == 1){
                $this -> success('添加成功','admin/admin/adminlist');
            } else {
                $this -> error($result);
            }
        }
        return view();
    }

    //管理员编辑
    public function admin_edit()
    {
        if (Request()->isAjax()){
            $data = input('post.');
            $result = model('common/Admin') -> admin_edit($data);
            if ($result == 1){
                $this -> success('编辑成功','admin/admin/adminlist');
            } else {
                $this -> error($result);
            }
        }
        $admin = model('common/Admin') -> find(input('id'));
        $data = [
            'admin' => $admin
        ];
        $this -> assign($data);
        return view('admin/adminedit');
    }

    //管理员密码编辑
    public function admin_pass()
    {
        if (Request() -> isAjax()){
            $result = model('common/Admin') -> admin_pass(input('post.'));
            if ($result == 1){
                $this -> success('编辑成功','admin/admin/adminlist');
            } else {
                $this -> error($result);
            }
        }
        $admin = model('common/Admin') -> find(input('id'));
        $data = [
            'admin' => $admin
        ];
        $this -> assign($data);
        return view('admin/adminpass');
    }

    //管理员删除
    public function admin_del()
    {
        $data = model('common/Admin') -> find(input('post.id'));
        $result = $data -> delete();
        if ($result){
            $this -> success('删除成功！','admin/admin/adminlist');
        } else {
            $this -> error( '删除失败！');
        }
    }

    //管理员状态控制
    public function admin_status()
    {
        $data = input('post.');
        $result = model('common/Admin') -> admin_status($data);
        if ($result == 1){
            $this -> success('操作成功！','admin/admin/adminlist');
        } else {
            $this -> error($result);
        }
    }



}
